import pymysql
import time
from datetime import datetime
from Common import sql_content

class Salary_place(object):
    def __init__(self):
        self.count = 0
        self.sql_content = sql_content.sql_content()
        self.salary_all = []
        # self.country = {'北京':'北京','上海':'上海' ,'天津':'天津','重庆':'重庆','黑龙江':'哈尔滨', '吉林':'长春',
        #                   '辽宁':'沈阳', '内蒙古':'呼和浩特','河北':'石家庄' ,'新疆':'乌鲁木齐', '甘肃':'兰州',
        #                   '青海':'西宁','陕西': '西安' ,'宁夏':'银川', '河南':'郑州' ,'山东':'济南','山西':'太原',
        #                   '安徽':'合肥', '湖南':'长沙','湖北':'武汉' ,'江苏':'南京','四川':'成都' ,'贵州':'贵阳',
        #                   '云南':'昆明', '广西':'南宁','西藏' :'拉萨' ,'浙江':'杭州', '江西':'南昌' ,'广东':'广州' ,
        # }
        self.country = {'北京':'beijing','上海':'sahnghai' ,'天津':'tianjin',
                        '长春':'changchun','沈阳':'shenyang','石家庄':'shijiazhuang',
                        '西宁':'xining','西安':'xian','银川':'yinchuan','郑州':'zhengzhou',
                        '济南':'jinan','太原':'taiyuan','合肥':'hefei','长沙':'changsha','武汉':'wuhan','南京':'nanjing','成都':'chengdu',
                        '昆明':'kunming','南宁':'nanning','杭州':'hangzhou',
                        '广州':'guangzhou'
        }
        # self.country = {'北京': 'beijing', '上海': 'sahnghai', '天津': 'tianjin', '重庆': 'chongqing', '黑龙江': 'heilongjiang',
        #                 '吉林': 'jilin',
        #                 '辽宁': 'liaoning', '内蒙古': 'neimenggu', '河北': 'hebei', '新疆': 'xinjiang', '甘肃': 'gansu',
        #                 '青海': 'qinghai', '陕西': 'shanxi3', '宁夏': 'ningxia', '河南': 'henan', '山东': 'shandong',
        #                 '山西': 'shanxi1',
        #                 '安徽': 'anhui', '湖南': 'hunan', '湖北': 'hubei', '江苏': 'jiangsu', '四川': 'sichuan', '贵州': 'guizhou',
        #                 '云南': 'yunnan', '广西': 'guangxi', '西藏': 'xizang', '浙江': 'zhejiang', '江西': 'jiangxi',
        #                 '广东': 'guangdong',
        #                 '哈尔滨': 'haerbin', '长春': 'changchun', '沈阳': 'shenyang', '呼和浩特': 'huhehaote',
        #                 '石家庄': 'shijiazhuang',
        #                 '乌鲁木齐': 'wulumuqi', '兰州': 'lanzhou', '西宁': 'xining', '西安': 'xian', '银川': 'yinchuan',
        #                 '郑州': 'zhengzhou',
        #                 '济南': 'jinan', '太原': 'taiyuan', '合肥': 'hefei', '长沙': 'changsha', '武汉': 'wuhan', '南京': 'nanjing',
        #                 '成都': 'chengdu',
        #                 '贵阳': 'guizhou', '昆明': 'kunming', '南宁': 'nanning', '拉萨': 'lasa', '杭州': 'hangzhou',
        #                 '南昌': 'nanchang',
        #                 '广州': 'guangzhou'
        #                 }
        self.salary_count_dict = {}
        self.atea_count_dict_new = {'other':0}
        self.this_dict = {}
#51社招Salary_Place
    def select_salary_51job(self):
        con = self.sql_content.conten()
        sql_51job = "select Job_Place,Job_Pay from data_51job_she"
        cursor = con.cursor()
        cursor.execute(sql_51job)
        rs = cursor.fetchall()
        for row in rs:
            #print(row)
            #print("salary_class-51所有工资=%s" % row)  # %---ceshi---%
            self.salary_all.append(row)
        cursor.close()
        con.close()


    def select_main(self):
        self.select_salary_51job()

        for one in self.country:
            count_country = 0
            for two in self.salary_all:
                if(two[0].count(one) > 0):
                    if(one not in self.salary_count_dict):
                        self.salary_count_dict[one] = []
                        self.salary_count_dict[one].append(two[1])
                    elif(one in self.salary_count_dict):
                        self.salary_count_dict[one].append(two[1])
        print('看看效果',len(self.country),len(self.salary_count_dict),self.salary_count_dict)

        for value in self.salary_count_dict:
            value_list_qian = []
            value_list_wan = []
            value_s_num = []
            value_e_num = []
            value_list_all = []
            for value_list in self.salary_count_dict[value]:
                if(value_list.find('千') > 0 ):
                    value_list_qian.append(value_list)
                elif(value_list.find('万') > 0 ):
                    value_list_wan.append(value_list)
            # print(value_list_qian)
            # print(value_list_wan)
            #从千到万
            if(len(value_list_qian) != 0 and len(value_list_wan) != 0):
                for qian in value_list_qian:
                    if(qian.find('-') < 0 ):
                        value_s_num.append(int(float(qian[:qian.find('千')])*1000))
                    else:
                        value_s_num.append(int(float(qian[:qian.find('-')])*1000))
                for wan in value_list_wan:
                    if (wan.find('-') < 0):
                        value_e_num.append(int(float(wan[:wan.find('万')]) * 1000))
                    else:
                        value_e_num.append(int(float(wan[wan.find('-') + 1:wan.find('-') + 2])*10000))
            elif (len(value_list_qian) != 0 and len(value_list_wan) == 0):
                for qian in value_list_qian:
                    value_s_num.append(int(float(qian[:qian.find('-')])*1000))
                    value_e_num.append(int(float(qian[qian.find('-') + 1:qian.find('-') + 2])*1000))
            elif(len(value_list_qian) == 0 and len(value_list_wan) != 0):
                for wan in value_list_wan:
                    value_s_num.append(int(float(wan[:wan.find('-')])*10000))
                    value_e_num.append(int(float(wan[wan.find('-') + 1:wan.find('-') + 2])*10000))

            #print('enen',value_s_num,value_e_num)
            value_s_num.sort()
            qian_state = value_s_num[0]
            value_e_num.sort()
            wan_end = value_e_num[-1]
            value_list_all.append(qian_state)
            value_list_all.append(wan_end)
            self.salary_count_dict[value] = value_list_all
            #print('waini',value_list_all)
            #self.salary_count_dict[value] = value_list_now
        print(len(self.country),len(self.salary_count_dict),self.salary_count_dict)

        return self.salary_count_dict

    def name_main(self):
        page = {}
        page['title'] = '部门城市工资情况'
        page['int'] = """
                Praesent mattis mauris urna. Suspendisse vulputate, urna sit amet laoreet iaculis, magna urna porttitor justo, vel pretium sapien turpis nec turpis. Integer nec consectetur odio. Mauris quis neque velit. Aenean nec metus leo, non pellentesque ero. Ut et elit nibh, sit amet rutrum leo. Nulla aliquet, sem vel faucibus tristique, mi erat gravida odio, et feugiat ipsum.

        Sed mi urna, adipiscing eget dignissim ac, dictum in nisl. Fusce id erat orci, ut placerat augue. Nulla tortor ligula, feugiat tincidunt accumsan ac, feugiat eget mi. Fusce euismod elit eget erat viverra pharetra. Aliquam tellus purus, convallis in tempor at, lacinia non lectus. Vivamus aliquet dictum volutpat. Sed sit amet urna tellus. Mauris id lacus arcu, nec volutpat odio. Sed risus urna, pulvinar vitae fermentum at, rutrum vitae arcu. Proin ante orci, congue quis molestie ac, ornare ac ipsum. Proin sit amet semper orci. Donec posuere, mi et tempus adipiscing, elit odio auctor eros, in fringilla quam velit sed dolor. Duis aliquam tristique nulla.
            """
        page['name'] = '部分城市工资情况'
        page['type'] = '毕业生,工资,待遇,图表,柱状图'
        page['unknow'] = 'view'
        page['date'] = '2017/8/27'
        page['list'] = {'小米招聘': 'www', '华为招聘': 'www', '阿里招聘': 'www', '百度招聘': 'www', '51招聘': 'www'}

        if(self.count != 0):
            pass
        elif(self.count == 0 or datetime.now().strftime('%H:%M') == '00:00'):
            Salary = Salary_place().select_main()
            self.count += 1

        return Salary,page

if __name__ == "__main__":
    Salary_place().select_main()

    #print(time.strftime('%Y-%m-%d', time.loadtime(time.time())))